// pages/notify/notify.js
var verify = require('../../../utils/verify-token.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
data: {
	date: '2019-11-13',
	list:[],	
	page:0,
	totalPage:0,
	loaded:false,
	loading:false,
	ind:-1,
	noticeTypeName:''
  },
   bindDateChange: function(e) {
    this.setData({
      date: e.detail.value,
	  page:0,
	  list:[]
    })
	this.getList();
  },
  getList:function(){
  	let thm = this;
  	let dat = {
  		date:thm.data.date,
  		size:10,
  		page:thm.data.page
  	};	

	wx.request({
	  url: app.globalData.baseUrl+'msg_notice/'+thm.data.noticeTypeId+'/list',
	  method: 'get',
	  header:{
		'AUTHORIZATION':app.globalData.token
	  },
	  data:dat,
	  success: function (res) {
		if (res.data.code == 60006) {
		  verify.clearAll()
		} else if(res.data.code==200){
		  let listdata = res.data.data.content;
		  let listD = thm.data.list;
		  let ldh = listD.length;
		  for(let i=0;i<listdata.length;i++){
			listD[ldh+i] = listdata[i]
		  }
		  thm.setData({
				list:listD,
				totalPage:res.data.data.totalPages,
				loading:res.data.data.last?false:true,
				loaded:res.data.data.last?true:false
		  })
		}
	  }
	})

  	
  	
  },
  gotoSetting:function(e){
	 wx.navigateTo({
	  url: 'notify-set/notify-set'
	})  
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {	
	let that = this;
	let noticeTypeId = options.ntid;
	let noticeTypeName = options.ntname;
	let day = new Date();
	let date = day.getFullYear() + '-' + ((day.getMonth() + 1) >= 10 ? (day.getMonth() + 1) : ('0' + (day.getMonth() + 1))) + '-' + (day.getDate() >= 10 ? day.getDate() : ('0' + day.getDate()));
	wx.setNavigationBarTitle({
	   title: noticeTypeName
	});
	that.setData({
		noticeTypeId:noticeTypeId,
	
		noticeTypeName:noticeTypeName,
		date:date
	})
	that.getList();
	
  },

  //读消息
  readMsg:function(e){	
	let  msgNoticeId = e.currentTarget.dataset.item.msgNoticeId;
	let that = this;
	that.setData({
		ind:e.currentTarget.dataset.index
	}) 
	
	wx.request({
		url:app.globalData.baseUrl+'msg_notice/'+msgNoticeId+'/reading',
		method:'post',
		header:{
			'AUTHORIZATION':app.globalData.token
		},
		success: (res) => {
			if(res.data.code==200){
				that.data.list[e.currentTarget.dataset.index].readStatus = true;
				that.setData({
					list:that.data.list
				}) 
			}else{
				wx.showModal({
					title:"失败提示",
					content:res.data.message
				})
			}
		}
	})


  },
  
  //全部已读
  allRead:function(){
	let that = this;	
	wx.request({
		url:app.globalData.baseUrl+'msg_notice/'+that.data.noticeTypeId+'/all_reading',
		method:'post',
		header:{
			'AUTHORIZATION':app.globalData.token
		},
		success: (res) => {
			if(res.data.code==200){
				that.setData({
					page:0,
					list:[]
				});
				that.getList()
			}
		}
	})

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
	wx.stopPullDownRefresh;
	//console.log(12)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
	let that=this;
	let page = that.data.page + 1; 
	if(page<=that.data.totalPage-1){	
		that.setData({
			page: page, 
		})					
		that.getList();
	}else{
		that.setData({
			loaded:true,
			loading:false
		})
	}
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})